#include "qlinewidget.h"

#include "../../property/qabstractproperty.h"
#include "../../property/qpropertyfactory.h"
#include "../../shared/qcommonstruct.h"

#include <QPainter>
#include <QGradient>

QLineWidget::QLineWidget(QWidget *parent) :
    QWidget(parent)
{
}

void QLineWidget::setLineWidth(int lineWidth)
{
    if(lineWidth<1)
    {
        lineWidth=1;
    }
    if(m_line_width!=lineWidth)
    {
        m_line_width=lineWidth;
        update();
    }
}

int QLineWidget::lineWidth()
{
    return m_line_width;
}

void QLineWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);

    int direction=property("direction").toInt();
    int line_style=property("line_style").toInt();
    if(direction==LD_LeftTop_RightBottom || direction==LD_LeftBottom_RightTop)
    {
        p.setRenderHint(QPainter::Antialiasing,true);
    }
    QGradient g=property("line_color").value<QGradient>();
    QPen pen((Qt::PenStyle)line_style);
    pen.setWidth(m_line_width);
    pen.setBrush(QBrush(g));
    p.setPen(pen);

    QPoint s,e;
    int off=m_line_width;
    if(direction==LD_Left_Right)
    {
        s=QPoint(0,height()/2);
        e=QPoint(width(),height()/2);
    }
    else if(direction==LD_Top_Bottom)
    {
        s=QPoint(width()/2,0);
        e=QPoint(width()/2,height());
    }
    else if(direction==LD_LeftTop_RightBottom)
    {
        s=QPoint(off,off);
        e=QPoint(width()-off,height()-off);
    }
    else if(direction==LD_LeftBottom_RightTop)
    {
        s=QPoint(off,height()-off);
        e=QPoint(width()-off,off);
    }
    else
    {
        s=QPoint(off,height()/2);
        e=QPoint(width()-off,height()/2);
    }

    p.drawLine(s,e);
}


QLineWidgetHost::QLineWidgetHost(QAbstractHost *parent):
    QWidgetHost(parent)
{
    this->setProperty("need_frame",true);
    setProperty("accept_drop",false);
}

void QLineWidgetHost::create_object()
{
    m_object=new QLineWidget;
    m_object->setObjectName("lineWidget");
}

QString QLineWidgetHost::get_show_name()
{
    return tr("Line");
}

QString QLineWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QLineWidgetHost::get_show_icon()
{
    return ":/images/line.png";
}

void QLineWidgetHost::init_property()
{
    QWidgetHost::init_property();

    remove_property("font");
    remove_property("enabled");
    remove_property("focusPolicy");
    remove_property("background");
    remove_property("border");
    remove_property("toolTip");
    remove_property("mouseMove");
    remove_property("mousePress");
    remove_property("mouseRelease");
    remove_property("doubleClick");

    QAbstractProperty* pro;

    pro=QPropertyFactory::create_property("Number");

    QLineWidget* wid=(QLineWidget*)m_object;
    if(pro!=NULL)
    {
        pro->set_property("name","line_width");
        pro->set_attribute("show_name",tr("Width"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_width",1);
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","line_color");
        pro->set_attribute("show_name",tr("Color"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_color",pro->get_value());
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","direction");
        pro->set_attribute("show_name",tr("Direction"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        ComboItems items;
        tagComboItem item;
        item.m_text="Left-Right";
        item.m_value=LD_Left_Right;
        items.append(item);
        item.m_text="Top-Bottom";
        item.m_value=LD_Top_Bottom;
        items.append(item);
        item.m_text="LeftTop-RightBottom";
        item.m_value=LD_LeftTop_RightBottom;
        items.append(item);
        item.m_text="LeftBottom-RightTop";
        item.m_value=LD_LeftBottom_RightTop;
        items.append(item);
        QVariant v;
        v.setValue<ComboItems>(items);
        pro->set_attribute("items",v);
        m_object->setProperty("direction",LD_Left_Right);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","line_style");
        pro->set_attribute("show_name",tr("Style"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        ComboItems items;
        tagComboItem item;
        item.m_text="SolidLine";
        item.m_value=Qt::SolidLine;
        items.append(item);
        item.m_text="DashLine";
        item.m_value=Qt::DashLine;
        items.append(item);
        item.m_text="DotLine";
        item.m_value=Qt::DotLine;
        items.append(item);
        item.m_text="DashDotLine";
        item.m_value=Qt::DashDotLine;
        items.append(item);
        item.m_text="DashDotDotLine";
        item.m_value=Qt::DashDotDotLine;
        items.append(item);
        QVariant v;
        v.setValue<ComboItems>(items);
        pro->set_attribute("items",v);
        m_object->setProperty("line_style",Qt::SolidLine);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,100,10));
}

void QLineWidgetHost::setDirection(int direction)
{
    set_property_value("direction",direction);
}

int QLineWidgetHost::direction()
{
    return get_property_value("direction").toInt();
}

void QLineWidgetHost::setLineWidth(int lineWidth)
{
    set_property_value("line_width",lineWidth);
}

int QLineWidgetHost::lineWidth()
{
    return get_property_value("line_width").toInt();
}

void QLineWidgetHost::setLineStyle(int lineStyle)
{
    set_property_value("line_style",lineStyle);
}

int QLineWidgetHost::lineStyle()
{
    return get_property_value("line_style").toInt();
}

qRegisterHost("basegraphics.line",QLineWidgetHost,BASE_GRAPHICS_INDEX)
